package com.winning.ds.role.service.impl;

import com.winning.ds.common.entity.MenuTreeEntity;
import com.winning.ds.common.entity.SysCzjsQxEntity;
import com.winning.ds.common.entity.TreeEntity;
import com.winning.ds.common.util.Common;
import com.winning.ds.common.util.UUIDUtils;
import com.winning.ds.role.dao.SysCzjsDao;
import com.winning.ds.role.entity.SysCzjsEntity;
import com.winning.ds.role.entity.SysCzjsGnsjqxEntity;
import com.winning.ds.role.entity.SysZcjgQxEntity;
import com.winning.ds.role.service.SysCzjsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: plat
 * @description:
 * @author: nxs
 * @create: 2018-07-05 09:34
 */
@Service("SysCzjsService")
public class SysCzjsServiceImpl implements SysCzjsService {
  @Autowired private SysCzjsDao sysCzjsDao;

  @Override
  @Transactional
  public void save(SysCzjsEntity sysczjs) {
    sysCzjsDao.insert(sysczjs);
  }

  @Override
  @Transactional
  public void update(SysCzjsEntity sysczjs) {
    sysCzjsDao.updateByPrimaryKey(sysczjs);
  }

  @Override
  public List<SysCzjsEntity> queryList(Map map) {

    // 如果不是超级管理员，则只查询自己所拥有的角色列表
    //  if("" != Constant.SUPER_ADMIN){
    //      map.put("createUserId", "");
    //  }
    List<SysCzjsEntity> list = sysCzjsDao.selectRoleList(map);
    return list;
  }

  @Override
  public List<SysCzjsEntity> queryListJgyhgl(Map map) {

    List<SysCzjsEntity> list = sysCzjsDao.selectRoleListJgyhgl(map);
    return list;
  }

  @Override
  public void deleteRole(String jsdm) {
    sysCzjsDao.deleteByPrimaryKey(jsdm);
  }

  @Override
  public SysCzjsEntity getRoleInfo(String jsdm) {
    return sysCzjsDao.selectByPrimaryKey(jsdm);
  }

  /**
   * 复制角色权限
   *
   * @param roleCheckList 选中要复制的角色
   * @param ztreeCheckList 要赋值的机构
   */
  @Override
  @Transactional
  public void copyRole(String roleCheckList, List ztreeCheckList) {
    // 选中的角色
    String[] rolelist = roleCheckList.split("\\|");
    for (int i = 0, len = rolelist.length; i < len; i++) {
      SysCzjsEntity sce = new SysCzjsEntity();
      SysCzjsEntity entity = sysCzjsDao.selectByPrimaryKey(rolelist[i].toString());
      sce.setJsmc(entity.getJsmc());
      sce.setJsms(entity.getJsms());
      // 查询当前角色权限
      List<SysCzjsQxEntity> list = getGnqxCheck(entity.getJgbm(), entity.getJsdm());

      for (int j = 0, ztreelen = ztreeCheckList.size(); j < ztreelen; j++) {
        sce.setJgbm(ztreeCheckList.get(j).toString());
        String uuid = UUIDUtils.getUUID();
        sce.setJsbm(uuid);
        sce.setJsdm(uuid);
        sysCzjsDao.insert(sce);
        List<SysCzjsQxEntity> czjsList = new ArrayList<>();
        for (SysCzjsQxEntity sysCzjsQxEntity : list) {
          sysCzjsQxEntity.setJgbm(ztreeCheckList.get(j).toString());
          sysCzjsQxEntity.setJsbm(uuid);
          sysCzjsQxEntity.setJsdm(uuid);
          sysCzjsQxEntity.setJsmc(sce.getJsmc());
          czjsList.add(sysCzjsQxEntity);
          if (czjsList.size() % 50 == 0) {
            sysCzjsDao.saveGnqx(czjsList);
            czjsList = new ArrayList<>();
          }
        }
        if (czjsList != null && czjsList.size() > 0) {
          sysCzjsDao.saveGnqx(czjsList);
        }
      }
    }
  }

  @Override
  public List<MenuTreeEntity> getMenuTree(String jgbm) {

    List<MenuTreeEntity> list = getZcjgqx(jgbm);
    List<MenuTreeEntity> menuList = null;
    List<MenuTreeEntity> gnanList = null;
    if (list.size() > 0) {
      // 获取菜单树
      menuList = sysCzjsDao.getMenuTree(jgbm);
      // 获取功能按钮
      //  gnanList = sysCzjsDao.getGnanList(jgbm);
    }
    menuList.addAll(gnanList);
    return menuList;
  }

  @Override
  public List<MenuTreeEntity> getMenu() {
    List<MenuTreeEntity> zcjgqxList = null;
    zcjgqxList = sysCzjsDao.getMenu();
    return zcjgqxList;
  }

  @Override
  public List<MenuTreeEntity> getZcjgqx(String jgbm) {
    List<MenuTreeEntity> gnanList = null;
    List<MenuTreeEntity> sjqxList = null;
    List<MenuTreeEntity> zcjgqxList = null;
    gnanList = sysCzjsDao.getGnanList(jgbm);
    sjqxList = sysCzjsDao.getSjqxList(jgbm);
    zcjgqxList = sysCzjsDao.getZcjgqx(jgbm);
    zcjgqxList.addAll(gnanList);
    zcjgqxList.addAll(sjqxList);
    return zcjgqxList;
  }

  // 保存机构权限设置
  @Override
  @Transactional
  public void saveJgqx(String jgqxCheckList, String jgbm) {
    List<MenuTreeEntity> list = sysCzjsDao.getZcjgqx(jgbm);
    if (list.size() > 0) {
      sysCzjsDao.delZcJgqx(jgbm);
    }

    if (!Common.validateParams(jgqxCheckList)) {
      return;
    }
    // 截取选中机构权限列表
    String[] jgqxlist = jgqxCheckList.split(",");

    // 根据机构编码查询该机构下 是否有 机构权限，有：删除再新增，无：直接新增

    List<SysZcjgQxEntity> listJgqx = new ArrayList<SysZcjgQxEntity>();
    for (int i = 0; i < jgqxlist.length; i++) {
      SysZcjgQxEntity szqe = new SysZcjgQxEntity();
      szqe.setLsh(UUIDUtils.getUUID());
      szqe.setJgbm(jgbm);
      szqe.setCddm(jgqxlist[i].toString());
      szqe.setMcode(jgqxlist[i].toString());
      szqe.setXzbz("0");
      listJgqx.add(szqe);
    }

    int count = 0;
    int totalNumer = 100; // 每次插入的条数
    int totalNumerInder = totalNumer; // 每次插入之后的下标
    for (int index = 0; index < listJgqx.size(); ) {
      if (totalNumerInder >= listJgqx.size()) {
        totalNumerInder = listJgqx.size();
        sysCzjsDao.saveJgqx(listJgqx.subList(index, totalNumerInder));
        count++;
        break; // 插入完跳出
      } else {
        sysCzjsDao.saveJgqx(listJgqx.subList(index, totalNumerInder)); // list 截取
        index = totalNumerInder; // 起点就是每次插入的最后一个点
        totalNumerInder = index + (totalNumer - 1); // 最后一个就是起点上插入的条数
        count++;
      }
    }
  }

  @Override
  @Transactional
  public void saveGnqx(String jgqxCheckList, String jsdm, String jgbm) {

    // 截取选中机构权限列表
    String[] gnqxlist = jgqxCheckList.split(",");
    List<SysCzjsQxEntity> list = sysCzjsDao.querySysCzjsQx(jgbm, jsdm);
    if (list.size() > 0) {
      sysCzjsDao.delSysCzjsQx(jgbm, jsdm);
    }
    List<SysCzjsQxEntity> czjsList = new ArrayList<>();
    for (int i = 0; i < gnqxlist.length; i++) {
      SysCzjsQxEntity scqe = new SysCzjsQxEntity();
      scqe.setJgbm(jgbm);
      scqe.setJsbm(jsdm);
      scqe.setJsdm(jsdm);
      scqe.setCddm(gnqxlist[i].toString());
      scqe.setQxlx((short) 1);
      scqe.setXzbz(0);
      scqe.setJsbm(jsdm);
      scqe.setMcode(gnqxlist[i].toString());
      czjsList.add(scqe);
      if (czjsList.size() % 50 == 0) {
        sysCzjsDao.saveGnqx(czjsList);
        czjsList = new ArrayList<>();
      }
    }
    if (czjsList != null && czjsList.size() > 0) {
      sysCzjsDao.saveGnqx(czjsList);
    }
  }

  @Override
  @Transactional
  public void saveJgSjqx(String sjqxCheckList, String jsdm, String jgbm) {

    // 截取选中机构权限列表
    String[] sjqxlist = sjqxCheckList.split(",");

    List<SysCzjsGnsjqxEntity> list = sysCzjsDao.getSjqxCheck(jgbm, jsdm);
    if (list.size() > 0) {
      sysCzjsDao.delSysCzjsSjqx(jgbm, jsdm);
      List<SysCzjsGnsjqxEntity> sjqxList = new ArrayList<>();
      for (int i = 0; i < sjqxlist.length; i++) {
        SysCzjsGnsjqxEntity scgse = new SysCzjsGnsjqxEntity();
        scgse.setLsh(UUIDUtils.getUUID());
        scgse.setJgbm(jgbm);
        scgse.setJsdm(jsdm);
        scgse.setGnsjdm(sjqxlist[i].toString());
        scgse.setQxlx("2");
        sjqxList.add(scgse);
      }
      sysCzjsDao.saveJgSjqx(sjqxList);
    } else {
      List<SysCzjsGnsjqxEntity> sjqxList = new ArrayList<>();
      for (int i = 0; i < sjqxlist.length; i++) {
        SysCzjsGnsjqxEntity scgse = new SysCzjsGnsjqxEntity();
        scgse.setLsh(UUIDUtils.getUUID());
        scgse.setJgbm(jgbm);
        scgse.setJsdm(jsdm);
        scgse.setGnsjdm(sjqxlist[i].toString());
        scgse.setQxlx("2");
        sjqxList.add(scgse);
      }
      sysCzjsDao.saveJgSjqx(sjqxList);
    }
  }

  @Override
  public List<SysCzjsQxEntity> getGnqxCheck(String jgbm, String jsdm) {

    return sysCzjsDao.getGnqxCheck(jgbm, jsdm);
  }

  @Override
  public List<SysCzjsGnsjqxEntity> getSjqxCheck(String jgbm, String jsdm) {
    return sysCzjsDao.getSjqxCheck(jgbm, jsdm);
  }

  @Override
  public void copyJgqx(String jgbm, List ztreeCheckList) {

    // 需要复制的 机构权限
    List<SysZcjgQxEntity> copylist = sysCzjsDao.querySysZcjgqx(jgbm);

    List<SysZcjgQxEntity> newList = new ArrayList<>();
    int num = 1; // 计数每200条保存一次
    for (int j = 0, ztreelen = ztreeCheckList.size(); j < ztreelen; j++) {
      List<SysZcjgQxEntity> list = sysCzjsDao.querySysZcjgqx(ztreeCheckList.get(j).toString());
      if (list.size() > 0) {
        sysCzjsDao.delZcJgqx(ztreeCheckList.get(j).toString());
      }
      for (SysZcjgQxEntity entity : copylist) {
        SysZcjgQxEntity szqe = new SysZcjgQxEntity();
        szqe.setJgbm(ztreeCheckList.get(j).toString());
        szqe.setLsh(UUIDUtils.getUUID());
        szqe.setCddm(entity.getCddm());
        szqe.setMcode(entity.getMcode());
        szqe.setXzbz("0");
        newList.add(szqe);
        num++;
        if (num % 200 == 0) {
          sysCzjsDao.saveJgqx(newList);
          num = 1;
          newList = new ArrayList<>();
        }
      }
    }
    sysCzjsDao.saveJgqx(newList);
  }

  @Override
  public List<SysZcjgQxEntity> querySysZcjgqx(String jgbm) {
    return sysCzjsDao.querySysZcjgqx(jgbm);
  }

  @Override
  public List<TreeEntity> getSjqzTree(String jgbm) {

    List<TreeEntity> idList = null;
    List<TreeEntity> pidList = null;
    idList = sysCzjsDao.getSjqxIdList(jgbm);
    pidList = sysCzjsDao.getSjqxPidList(jgbm);

    List<String> idListNew = idList.stream().map(TreeEntity::getpId).collect(Collectors.toList());
    for (TreeEntity pid : pidList) {
      if (idListNew.contains(pid.getId())) {
        idList.add(pid);
      }
    }
    return idList;
  }
}
